<script setup lang="tsx">
import {
  ApiSysCodeGenerationInput,
  SyscgconfigApi,
  SyscodegenerationApi,
} from "@/api-services";
import { feature, getAPI } from "@/axios-utils";
import { $t } from "@/locales";
import { queryDone } from "@/utils/api";
import { onMounted, ref } from "vue";

// interface codeGeneration {
//   cgConfigId: number;
// }

const codeGenerationModel = ref<ApiSysCodeGenerationInput>({
  cgConfigId: 0,
});

const configSelectItems = ref<any[]>([]);
async function GetConfigSelectItems() {
  const [err, res] = await feature(getAPI(SyscgconfigApi).getSelectItems());
  queryDone(err, () => {
    if (res?.data.data) {
      //console.log(res.data.data);
      configSelectItems.value = res.data.data.map((it) => ({
        value: it.key,
        label: it.value,
      }));
      if (configSelectItems.value.length > 0) {
        //如果是开环境则打印日志
        if (process.env.NODE_ENV === "development") {
          console.log(configSelectItems.value[0].value);
        }
        codeGenerationModel.value.cgConfigId = configSelectItems.value[0].value;
      }
    }
  });
}
onMounted(() => {
  GetConfigSelectItems();
});
const loading = ref(false);
/**
 * 生成代码
 */
async function handleCodeGeneration() {
  if (process.env.NODE_ENV === "development")
    console.log(codeGenerationModel.value);

  loading.value = true;
  const [err, res] = await feature(
    getAPI(SyscodegenerationApi).codeGeneration(codeGenerationModel.value),
  );
  loading.value = false;
  queryDone(err, async () => {
    if (res?.data.succeeded) {
      window.$message?.success($t("common.updateSuccess"));
    }
  });
}
</script>
<template>
  <div style="background: #ececec; padding: 30px">
    <ElCard title="代码生成" :bordered="false" style="width: 50%" size="small">
      <ElForm ref="formRef" :model="codeGenerationModel" label-width="auto">
        <ElFormItem label="配置项" name="configId">
          <ElSelect v-model="codeGenerationModel.cgConfigId">

            <ElOption v-for="item in configSelectItems" :key="item.value" :label="item.label" :value="item.value">
            </ElOption>

          </ElSelect>
        </ElFormItem>
      </ElForm>
      <ElButton type="primary" :loading="loading" @click="handleCodeGeneration">{{
        $t("page.codeGenerator.sysCodeGeneration.buttonCodeGeneration")
      }}</ElButton>
    </ElCard>
  </div>
</template>

<style scoped></style>
